/* *****************************************************************************
 * JFab (http://code.google.com/p/jfab)
 * Copyright (c) 2011 JFab.org
 * Admin jfab@jeffreythompson.net
 *
 * See the file "LICENSE.txt" for information on usage and redistribution of
 * this file, and for a DISCLAIMER OF ALL WARRANTIES.
 * *****************************************************************************/


package org.jfab.core.comparator;

import org.jfab.core.model.CGParameter;

import java.util.Comparator;


/**
 * Provides a code generator parameter comparator.
 */
public class CGParameterComparator implements Comparator<CGParameter>
{
    /**
     * @see  java.util.Comparator#compare(java.lang.Object, java.lang.Object)
     */
    @Override public int compare(CGParameter object1, CGParameter object2)
    {
        int answer = -1;

        if (object1 == object2)
        {
            answer = 0;
        }
        else if (object1 == null)
        {
            answer = 1;
        }
        else if (object2 == null)
        {
            answer = -1;
        }
        else
        {
            String name1 = object1.getName();
            String name2 = object2.getName();

            if (name1 == name2)
            {
                answer = 0;
            }
            else if (name1 == null)
            {
                answer = 1;
            }
            else if (name2 == null)
            {
                answer = -1;
            }
            else
            {
                answer = name1.compareTo(name2);
            }
        }

        return answer;
    }
}
